const express = require("express");
const path = require("path");
const app = express();

// 配置静态文件服务（重点配置）
app.use(
    "/webpack-react-start",
    express.static(path.join(__dirname, "dist"), {
        index: false, // 禁用自动响应index.html
    })
);

// 设置静态文件目录
const distPath = path.join(process.cwd(), "src", "public", "dist", "webpack-react-start正确");
console.log("静态文件目录:", distPath);

// 提供静态文件
app.use("/webpack-react-start", express.static(distPath));

// 处理所有路由请求（支持前端路由）
app.get("/webpack-react-start/*", (req, res) => {
    res.sendFile(path.join(distPath, "dist", "index.html"));
});

// 根路径重定向到应用
app.get("/", (req, res) => {
    res.redirect("/webpack-react-start/");
});

// 启动服务
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}/webpack-react-start/`);
});
